package com.xuhuan.demo.vo;

import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.Digits;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: 徐欢
 * @date: 2021/8/4 14:52
 * @description: 用户信息vo
 */
@Data
public class UserVO {

    /**
     * id
     */
    @NotNull(message = "用户id不能为空")
    private Integer userId;

    /**
     * 姓名
     */
    @Length(min = 1,max = 4,message = "姓名长度在1位到4位之间")
    private String name;

    /**
     * 年龄
     */
    @Min(value = 0,message = "年龄最小1岁")
    @Max(value = 100,message = "年龄最大100岁")
    @Digits(integer = 2,fraction = 0,message = "年龄必须为整数")
    private Integer age;


    public static void main(String[] args){
        List<Map<String,Object>> dataList=new ArrayList<>();
        Map map1=new HashMap(2);
        map1.put("id",1);
        map1.put("data","2021-08-12");
        dataList.add(map1);

        Map map2=new HashMap(2);
        map2.put("id",1);
        map2.put("data","2021-08-10");
        dataList.add(map2);

        Map map3=new HashMap(2);
        map3.put("id",2);
        map3.put("data","2021-08-12");
        dataList.add(map3);

        Map map4=new HashMap(2);
        map4.put("id",2);
        map4.put("data","2021-08-13");
        dataList.add(map4);

        //分组
        Map<Integer, List<Map<String, Object>>> resultMap = dataList.stream().collect(Collectors.groupingBy(map -> (int) map.get("id")));
        System.out.println(resultMap);

        List<Map> resultMapList=new ArrayList<>();

        Set<Map.Entry<Integer, List<Map<String, Object>>>> entries = resultMap.entrySet();
        for(Map.Entry<Integer, List<Map<String, Object>>> map:entries){
            List<Map<String,Object>> value = map.getValue();
            List<Map<String,Object>> sortDataList = value.stream()
                    .sorted(Comparator.comparing((Map<String, Object> h) -> (String) h.get("data")).reversed())
                    .collect(Collectors.toList());

            resultMapList.add(sortDataList.get(0));
        }

        System.out.println(resultMapList);
    }
}
